import { defineConfig ,ConfigEnv,loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import JSX from '@vitejs/plugin-vue-jsx'
import postcssNested from 'postcss-nested'
import { resolve } from 'path'


export default defineConfig(({ command, mode }: ConfigEnv) => {
  const root = process.cwd();
  const env = loadEnv(mode, root);
  // console.log(env);
  
  if (command === 'serve') {
    // dev模式
    return {
      resolve: {
        alias: {
          "@": resolve(__dirname, "./src"),
        },
      },
      plugins: [vue(), JSX()],
      css: {
        postcss: {
          plugins: [postcssNested]
        },
        preprocessorOptions: {
          sass: {
            javascriptEnabled: true,
            additionalData: `@import "${resolve(__dirname, 'src/styles/index.scss')}"`
          }
        }
      },
      server: {
        proxy: {
          '/api': {
            target: env.VITE_BASE_URL,    // 后台服务器地址
            changeOrigin: true,
            rewrite: (path) => path.replace(/^\/api/, '')
          },
        }
      }
    }
  } else {
    // production模式
    return {

    }
  }


})
